#!/usr/bin/make

#defaults
LIB = flap

#main building variables
DOBJ    = static/obj/
DMOD    = static/mod/
DEXE    = static/
MAKELIB = ar -rcs $(DEXE)lib$(LIB).a $(DOBJ)*.o ; ranlib $(DEXE)lib$(LIB).a
DSRC    = src
FC      = gfortran
OPTSC   = -cpp -c -frealloc-lhs -O3 -J static/mod
OPTSL   = -J static/mod
VPATH   = $(DSRC) $(DOBJ) $(DMOD)
MKDIRS  = $(DOBJ) $(DMOD) $(DEXE)

#auxiliary variables
COTEXT  = "Compiling $(<F)"
LITEXT  = "Assembling $@"

#building rule
$(LIB): $(MKDIRS) $(DOBJ)$(LIB).o
	@echo $(LITEXT)
	@$(MAKELIB)

#compiling rules
$(DOBJ)flap_command_line_interface_t.o: src/FLAP/flap_command_line_interface_t.F90 \
	$(DEXE)libface.a \
	$(DOBJ)flap_command_line_argument_t.o \
	$(DOBJ)flap_command_line_arguments_group_t.o \
	$(DOBJ)flap_object_t.o \
	$(DOBJ)flap_utils_m.o \
	$(DEXE)libpenf.a
	@echo $(COTEXT)
	@$(FC) $(OPTSC) $< -o $@

$(DOBJ)flap_command_line_arguments_group_t.o: src/FLAP/flap_command_line_arguments_group_t.f90 \
	$(DEXE)libface.a \
	$(DOBJ)flap_command_line_argument_t.o \
	$(DOBJ)flap_object_t.o \
	$(DEXE)libpenf.a
	@echo $(COTEXT)
	@$(FC) $(OPTSC) $< -o $@

$(DOBJ)flap_utils_m.o: src/FLAP/flap_utils_m.f90 \
	$(DEXE)libpenf.a
	@echo $(COTEXT)
	@$(FC) $(OPTSC) $< -o $@

$(DOBJ)flap_command_line_argument_t.o: src/FLAP/flap_command_line_argument_t.F90 \
	$(DEXE)libface.a \
	$(DOBJ)flap_object_t.o \
	$(DOBJ)flap_utils_m.o \
	$(DEXE)libpenf.a
	@echo $(COTEXT)
	@$(FC) $(OPTSC) $< -o $@

$(DOBJ)flap_object_t.o: src/FLAP/flap_object_t.f90 \
	$(DEXE)libpenf.a
	@echo $(COTEXT)
	@$(FC) $(OPTSC) $< -o $@

$(DOBJ)flap.o: src/FLAP/flap.f90 \
	$(DOBJ)flap_command_line_argument_t.o \
	$(DOBJ)flap_command_line_arguments_group_t.o \
	$(DOBJ)flap_command_line_interface_t.o
	@echo $(COTEXT)
	@$(FC) $(OPTSC) $< -o $@

.NOTPARALLEL: $(DEXE)libface.a $(DEXE)libpenf.a

$(DEXE)libface.a:
	@$(MAKE) -f makefile.face

$(DEXE)libpenf.a:
	@$(MAKE) -f makefile.penf

#phony auxiliary rules
.PHONY : $(MKDIRS)
$(MKDIRS):
	@mkdir -p $@
.PHONY : cleanobj
cleanobj:
	@echo deleting objects
	@rm -fr $(DOBJ)
.PHONY : cleanmod
cleanmod:
	@echo deleting mods
	@rm -fr $(DMOD)
.PHONY : clean
clean: cleanobj cleanmod
